package com.younge.spring.controller;

import com.younge.spring.api.common.PageResult;
import com.younge.spring.api.param.*;
import com.younge.spring.api.result.MenuListResult;
import com.younge.spring.repository.dao.Demo;
import com.younge.spring.repository.dao.SysRole;
import com.younge.spring.repository.dao.SysUser;
import com.younge.spring.service.SysMenuService;
import com.younge.spring.service.SysRoleService;
import com.younge.spring.service.SysService;
import com.younge.spring.service.SysUserService;
import jakarta.annotation.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping(path = "/web/system")
public class SysContorller {
    private final SysService sysService;
    private final SysUserService sysUserService;
    private final SysMenuService sysMenuService;
    private final SysRoleService sysRoleService;

    public SysContorller(SysService sysService, SysUserService sysUserService, SysMenuService sysMenuService,
                         SysRoleService sysRoleService) {
        this.sysService = sysService;
        this.sysUserService = sysUserService;
        this.sysMenuService = sysMenuService;
        this.sysRoleService = sysRoleService;
    }

    @GetMapping("/table/list")
    public PageResult<Demo> getTableList(@ModelAttribute TableListParam tableListParam) {
        return PageResult.of(sysService.getTableList(tableListParam));
    }

    @PostMapping("/user/add")
    public String addUser(@ModelAttribute UserAddParam userAddParam) {
        return sysUserService.addUser(userAddParam);
    }

    @GetMapping("/user/list")
    public PageResult<SysUser> getUserList(@ModelAttribute UserListParam userListParam) {
        return PageResult.of(sysUserService.getUserList(userListParam));
    }

    @GetMapping("/menu/list")
    public List<MenuListResult> getUserList() {
        return sysMenuService.getMenuList(null, false);
    }

    @GetMapping("/menu/name-exists")
    public boolean menuNameExists(@RequestParam @Nullable Long id, @RequestParam @Nullable String name) {
        if (StringUtils.hasText(name)) {
            return sysMenuService.isMenuNameExists(id, name);
        } else {
            return false;
        }
    }

    @GetMapping("/menu/path-exists")
    public boolean menuPathExists(@RequestParam @Nullable Long id, @RequestParam @Nullable String path) {
        if (StringUtils.hasText(path)) {
            return sysMenuService.isMenuPathExists(id, path);
        } else {
            return false;
        }
    }

    @PutMapping("/menu/{id}")
    public String updateMenu(@PathVariable Long id, @RequestBody MenuUpdateParam param) {
        if (null != param.getId() && !id.equals(param.getId())) {
            throw new RuntimeException("菜单ID不匹配");
        } else {
            param.setId(id);
        }
        return sysMenuService.updateMenu(param);
    }

    @PostMapping("/menu")
    public String createMenu(@RequestBody MenuUpdateParam param) {
        return sysMenuService.createMenu(param);
    }

    @GetMapping("/role/list")
    public PageResult<SysRole> getRoleList(@ModelAttribute RoleListParam param) {
        return PageResult.of(sysRoleService.getRoleList(param));
    }

    @GetMapping("/role/permissions/{id}")
    public Set<Long> getRolePermissions(@PathVariable Long id) {
        return sysRoleService.getRolePermissions(id);
    }

    @PostMapping("/role")
    public String createMenu(@RequestBody SysRole param) {
        return sysRoleService.createRole(param);
    }

    @PutMapping("/role/{id}")
    public String updateMenu(@PathVariable Long id, @RequestBody RoleUpdateParam param) {
        if (null != param.getId() && !id.equals(param.getId())) {
            throw new RuntimeException("ID不匹配");
        }
        param.setId(id);
        return sysRoleService.updateRole(param);
    }
}

